package org.swqsbssddqs.shortlink.admin.common.biz.user;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.swqsbssddqs.shortlink.admin.common.convention.exception.ClientException;

import java.io.IOException;
import java.util.HashSet;

import static org.swqsbssddqs.shortlink.admin.common.constant.RedisCacheConstant.USER_LOGIN_KEY;
import static org.swqsbssddqs.shortlink.admin.common.enums.UserErrorCodeEnum.USER_TOKEN_FAIL;


@Component
@Slf4j
@RequiredArgsConstructor
public class UserTransmitFilter implements Filter {
        //private final StringRedisTemplate stringRedisTemplate;
        static HashSet ignore_path =new HashSet<String>(){{
            add("/api/short-link/admin/v1/user/login");
            add("/api/short-link/admin/v1/user/has-username");
            add("/api/short-link/admin/v1/title");
        }};
    @SneakyThrows
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) {
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        log.info("访问url:{}",httpServletRequest.getRequestURL());
        String username = httpServletRequest.getHeader("username");
        if (StrUtil.isNotBlank(username)) {
            String userId = httpServletRequest.getHeader("userId");
            String realName = httpServletRequest.getHeader("realName");
            UserInfoDTO userInfoDTO = new UserInfoDTO(userId, username, realName);
            UserContext.setUser(userInfoDTO);
        }
        try {
            filterChain.doFilter(servletRequest, servletResponse);
        } finally {
            UserContext.removeUser();
        }
    }

//    @Override
//        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//            HttpServletRequest request = (HttpServletRequest)servletRequest;
//            HttpServletResponse response = (HttpServletResponse)servletResponse;
//            String username = request.getHeader("username");
//            String token = request.getHeader("token");
//            try{
//                if(!ignore_path.contains(request.getRequestURI())){
//                    if(!(request.getRequestURI().equals("/api/short-link/admin/v1/user")&&request.getMethod().equals("POST"))){
//                        if(!StrUtil.isAllNotBlank(username,token)){
//                            throw new ClientException(USER_TOKEN_FAIL);
//                        }
//                        Object o = stringRedisTemplate.opsForHash().get(USER_LOGIN_KEY + username, token);
//                        if(o!=null){
//                            UserInfoDTO userInfoDTO = JSON.parseObject(o.toString(), UserInfoDTO.class);
//                            UserContext.setUser(userInfoDTO);
//                        }else {
//                            throw new ClientException(USER_TOKEN_FAIL);
//                        }
//                    }
//
//                }
//                filterChain.doFilter(request,response);
//            }finally {
//                UserContext.removeUser();
//            }
//        }

}
